package com.zxn.rv;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zxn.divider.RvItemDecoration;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.app.Activity.RESULT_OK;


/**
 * Created by zxn on 2020-2-22 9:57:36.
 */
public class RvFrag extends Fragment {


    private static final String ARG_PARAM1 = "arg_param1";
    @BindView(R.id.rv)
    RecyclerView rv;
    //    @BindView(R.id.rv)
//    RecyclerView rv;
    private String mParam1;
    private KMAdapter mAdapter;
    private static int REQUEST_CODE = 1001;
    TextView numView;

    public RvFrag() {
    }

    public static RvFrag newInstance(String param1) {
        RvFrag fragment = new RvFrag();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    public static RvFrag newInstance() {
        RvFrag fragment = new RvFrag();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_rv, null);
        ButterKnife.bind(this, view);
        return view;
    }

    /*@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(view);
    }*/

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        onInitRecyclerView();
    }

    private void onInitRecyclerView() {
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
        rv.setLayoutManager(layoutManager);
        rv.setHasFixedSize(true);
        RvItemDecoration decoration = new RvItemDecoration.Builder(getContext()).bgColor(R.color.c_ffffff)
                .widthDp(10f)
                .spanCount(layoutManager.getSpanCount())
                .headCount(1)
                .createHeadGrid();
        rv.addItemDecoration(decoration);
        mAdapter = new KMAdapter();
        View header = View.inflate(getActivity(), R.layout.header_rv, null);
        mAdapter.addHeaderView(header);
        rv.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mAdapter.setmCheckedPositon(position);
                //notifyItemChanged(position);
                mAdapter.notifyDataSetChanged();
                setHeaderView(header, position);

            }
        });

        ArrayList<BaseInfo> data = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            if (i < 3) {
                data.add(new BaseInfo(false, (i + 10)));
            } else {
                data.add(new BaseInfo(true, (i + 10)));
            }
        }
        mAdapter.setNewData(data);
    }

    private void setHeaderView(View header, int position) {
        numView = header.findViewById(R.id.tv_num);
        TextView customView = header.findViewById(R.id.tv_custom);
        numView.setText(String.valueOf(mAdapter.getData().get(position).num));

        customView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), InPutNumberActivity.class);
                startActivityForResult(intent, 1);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    String returnData = data.getStringExtra("data");
                    Log.d(" -- returnData --", returnData);
                    numView.setText(returnData);
                }
                break;
        }
    }
}
